// getAlbumDetailData/index.js

const cloud = require('wx-server-sdk');
cloud.init();

const db = cloud.database();
const collection = db.collection('album');

exports.main = async (event, context) => {
  try {
    const { type } = event;

    console.log('Fetching album detail data for type:', type);

    const result = await collection.where({ type }).get();

    if (result.data && result.data.length > 0) {
      console.log('Album detail data found:', result.data[0]);
      return {
        title: result.data[0].title || '',
        text: result.data[0].content || '', // 使用 content 字段作为详细内容
        images: [{ src: result.data[0].imageUrl, title: result.data[0].title }], // 使用 imageUrl 字段作为图片地址
      };
    } else {
      console.log('No album detail data found for type:', type);
      return {
        title: '',
        text: '',
        images: [],
      };
    }
  } catch (error) {
    console.error('Error fetching album detail data:', error);
    throw new Error('Failed to fetch album detail data from cloud');
  }
};
